package application.mvc.biz.message;

import java.net.URL;
import java.util.ResourceBundle;

import com.jfinal.kit.StrKit;

import application.common.constant.SceneType;
import application.common.ui.ProgressStage;
import application.common.util.LayerUtil;
import application.common.util.RouteUtil;
import application.common.util.ShiroUtil;
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.util.Duration;

public class MessageController implements Initializable {

	
	@FXML
	private Label messageLabel;
	
	@FXML
	private Label headerLabel;
	
	@FXML
	private Label copyrightLabel;
	
	@FXML
	private ImageView portraitImageView;
	
	@FXML
	private Label usernameLabel;
	
	@FXML
	private Label roleNameLabel;
	
	@FXML
	private Pane timerOutLinePane;
	@FXML
	private Pane timerInLinePane;
	@FXML
	private Label timerLabel;
	private Timeline animation;
	private RotateTransition rotateTransitionOut;
	private RotateTransition rotateTransitionIn;
	private boolean isDestroy;

    private IntegerProperty type = new SimpleIntegerProperty();
    private StringProperty message = new SimpleStringProperty();
    private StringProperty srcScene = new SimpleStringProperty();

	
	public MessageController(int type,String message,String srcPage) {
		this.type.set(type);
		this.message.set(message);
		this.srcScene.set(srcPage);
	}
	
    
	public void initialize(URL location, ResourceBundle resources) {
		headerLabel.setText(ShiroUtil.getSoftName());
		copyrightLabel.setText(ShiroUtil.getCopyright());
		portraitImageView.setImage(ShiroUtil.getUser().getPortrait());
		usernameLabel.setText(ShiroUtil.getUser().getName());
		roleNameLabel.setText(ShiroUtil.getUser().getRoleName());
		message(message.get(), false);
		
		if(!isDestroy) {
			rotateTransitionOut = new RotateTransition(Duration.seconds(10), timerOutLinePane);
			rotateTransitionOut.setFromAngle(0);
			rotateTransitionOut.setToAngle(360);
			rotateTransitionOut.setByAngle(360); // 设置旋转的角度
			rotateTransitionOut.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
			rotateTransitionOut.setInterpolator(Interpolator.LINEAR);
			rotateTransitionOut.play();

			rotateTransitionIn = new RotateTransition(Duration.seconds(10), timerInLinePane);
			rotateTransitionIn.setFromAngle(360);
			rotateTransitionIn.setToAngle(0);
			rotateTransitionIn.setByAngle(360); // 设置旋转的角度
			rotateTransitionIn.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
			rotateTransitionIn.setInterpolator(Interpolator.LINEAR);
			rotateTransitionIn.play();
			startCountDown();
		}
	}
	
	public void startCountDown() {
		animation = new Timeline(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				if(RouteUtil.getTime()>0) {
					RouteUtil.setTime(RouteUtil.getTime() - 1);
					if (RouteUtil.getTime() == 0) {
						timerLabel.setText("0");
						LayerUtil.open("操作超时！", 999);
					} else if (RouteUtil.getTime() > 0) {
						timerLabel.setText(RouteUtil.getTime() + "");
					}
				}
				
			}
		}));
		animation.setCycleCount(Timeline.INDEFINITE);
		animation.play();
	}

	public void destroy() {
		if (animation != null) {
			animation.stop();
		}
		if (rotateTransitionOut != null) {
			rotateTransitionOut.stop();
		}
		if (rotateTransitionIn != null) {
			rotateTransitionIn.stop();
		}
	}
	
	public void exit(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			ProgressStage.of(RouteUtil.mainStage, new Task<Void>() {
				@Override
				protected Void call() throws Exception {
					destroy();
					RouteUtil.goIndex();
					return null;
				}
			},"退出中...").show();
		}
	}

	public void next(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			Button bu = (Button) event.getSource();
			bu.setVisible(false);
			ProgressStage.of(RouteUtil.mainStage, new Task<Void>() {
				@Override
				protected Void call() throws Exception {
					destroy();
					if(StrKit.equals(SceneType.CABINET, srcScene.get())) {
						RouteUtil.goArchives();
					}
					else if(StrKit.equals(SceneType.ARCHIVES, srcScene.get())) {
						RouteUtil.goIndex();
					}
					else if(StrKit.equals(SceneType.SCANNER, srcScene.get())) {
						RouteUtil.goArchives();
					}
					else if(StrKit.equals(SceneType.CABGROUP, srcScene.get())) {
						RouteUtil.goArchives();
					}else {
						RouteUtil.goIndex();
					}
					return null;
				}
			},"跳转页面中...").show();
		}
	}
	
	private void message(final String text, final boolean isOk) {
		Platform.runLater(new Runnable() {
			public void run() {
				if (isOk) {
					messageLabel.setId("message-label");
				} else {
					messageLabel.setId("message-label-error");
				}
				messageLabel.setText(text);
			}
		});
	}

}
